/**
 * 初始化FulltimeTrainPoints详情对话框
 */
var FulltimeTrainPointsInfoDlg = {
    fulltimeTrainPointsInfoData : {}
};

/**
 * 清除数据
 */
FulltimeTrainPointsInfoDlg.clearData = function() {
    this.fulltimeTrainPointsInfoData = {};
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
FulltimeTrainPointsInfoDlg.set = function(key, val) {
    this.fulltimeTrainPointsInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
FulltimeTrainPointsInfoDlg.get = function(key) {
    return $("#" + key).val();
};

/**
 * 关闭此对话框
 */
FulltimeTrainPointsInfoDlg.close = function() {
    parent.layer.close(window.parent.FulltimeTrainPoints.layerIndex);
};

/**
 * 收集数据
 */
FulltimeTrainPointsInfoDlg.collectData = function() {
    this
        .set('id')
        .set('empName')
        .set('empCode')
        .set('startTime')
        .set('endTime')
        .set('projectLevel')
        .set('checkResult')
        .set('trainingCourse')
        .set('trainsSponsor')
        .set('trainsAddress')
        .set('finishHours')
        .set('credit')
        .set('originData')
        .set('createDate')
        .set('year')
        .set('twoLevel')
        .set('threeLevel')
        .set('testResults')
    ;
};

/**
 * 提交添加
 */
FulltimeTrainPointsInfoDlg.addSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }
    if(!$("#year").val()){
        Feng.error("年度不能为空!!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    if(!$("#empName").val()){
        Feng.error("员工不能为空!!");
        $("#ensure").removeAttr("disabled");
        return ;
    }

    if(!$("#finishHours").val()){
        Feng.error("完成学时不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }

    if(!$("#startTime").val()){
        Feng.error("开始时间不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    if(!$("#endTime").val()){
        Feng.error("结束时间不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }

    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var finishHours = $("#finishHours").val();
    var subDate = compareDays(startTime,endTime);

    if(finishHours && finishHours > subDate*8){
        Feng.error("完成学时不能大于"+ subDate*8 + "学时！");
        $("#ensure").removeAttr("disabled");
        return;
    }

    var args = {};
    args.empCode = $("#empCode").val();
    args.startDate = $("#startTime").val();
    $.ajax({
        type: "post",
        url: Feng.ctxPath + "/fulltimeTrainPoints/checkDate",
        dataType: "json",
        data: args,
        success: function(data) {
            if(data.code == 500){
                Feng.error(data.msg);
                $("#ensure").removeAttr("disabled");
                return;
            }else{
                FulltimeTrainPointsInfoDlg.clearData();
                FulltimeTrainPointsInfoDlg.collectData();

                //提交信息
                var ajax = new $ax(Feng.ctxPath + "/fulltimeTrainPoints/add", function(data){
                    Feng.success("添加成功!");
                    window.parent.FulltimeTrainPoints.table.refresh();
                    FulltimeTrainPointsInfoDlg.close();
                },function(data){
                    Feng.error("添加失败!" + data.responseJSON.message + "!");
                });
                ajax.set(FulltimeTrainPointsInfoDlg.fulltimeTrainPointsInfoData);
                ajax.start();
            }
        }
    });



};

/**
 * 提交修改
 */
FulltimeTrainPointsInfoDlg.editSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }
    if(!$("#year").val()){
        Feng.error("年度不能为空!!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    if(!$("#empName").val()){
        Feng.error("员工不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }

    if(!$("#finishHours").val()){
        Feng.error("完成学时不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }

    if(!$("#startTime").val()){
        Feng.error("开始时间不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    if(!$("#endTime").val()){
        Feng.error("结束时间不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }

    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var finishHours = $("#finishHours").val();
    var subDate = compareDays(startTime,endTime);

    if(finishHours && finishHours > subDate*8){
        Feng.error("完成学时不能大于"+ subDate*8 + "学时！");
        $("#ensure").removeAttr("disabled");
        return;
    }

    var args = {};
    args.empCode = $("#empCode").val();
    args.startDate = $("#startTime").val();
    args.id = $("#id").val();
    $.ajax({
        type: "post",
        url: Feng.ctxPath + "/fulltimeTrainPoints/checkDate",
        dataType: "json",
        data: args,
        success: function(data) {
            if(data.code == 500){
                Feng.error(data.msg);
                $("#ensure").removeAttr("disabled");
                return;
            }else{
                FulltimeTrainPointsInfoDlg.clearData();
                FulltimeTrainPointsInfoDlg.collectData();

                //提交信息
                var ajax = new $ax(Feng.ctxPath + "/fulltimeTrainPoints/update", function(data){
                    Feng.success("修改成功!");
                    window.parent.FulltimeTrainPoints.table.refresh();
                    FulltimeTrainPointsInfoDlg.close();
                },function(data){
                    Feng.error("修改失败!" + data.responseJSON.message + "!");
                });
                ajax.set(FulltimeTrainPointsInfoDlg.fulltimeTrainPointsInfoData);
                ajax.start();
            }
        }
    });
};



FulltimeTrainPointsInfoDlg.twoLevelChange = function () {

    $('#twoLevel').change(function(){
        var selected = $("#twoLevel option:selected");
        var twoLevelName = selected.text();
        //获取竞赛类型
        var ajax = new $ax(Feng.ctxPath + "/fulltimeTrainPoints/threeLevelName?twoLevelName="+twoLevelName, function(data){
            if(data.data){

                $("#threeLevel").empty();

                var rules = data.data;
                var r, str_html;
                for(r in rules){
                    str_html = "<option value='"+rules[r].name+"'>"+rules[r].name+"</option>";
                    $("#threeLevel").append(str_html);
                }
            }
        });
        ajax.start();
    });

};



/**
 * 规则计算
 */
FulltimeTrainPointsInfoDlg.ruleCal = function () {
    $("#finishHours").blur(function(){
        var finishHours = $("#finishHours").val();
        var empCode = $("#empCode").val();
        if(!empCode){
            Feng.alert("请先选择员工!");
            $("#finishHours").val("");
            return;
        }

        var args = {};
        args.hours = finishHours;
        args.empCode = empCode;

        $.ajax({
            type: "post",
            url: Feng.ctxPath + "/fulltimeTrainPoints/integralCal",
            dataType: "json",
            data: args,
            success: function(data) {

                if(data.code == 500){
                    Feng.error(data.message);
                }else{
                    $("#credit").val(data.data);
                }
            }
        });

    });
};

/**
 * 上传提交
 */
FulltimeTrainPointsInfoDlg.upload = function () {
    layui.use('upload', function(){
        var upload = layui.upload;
        //固定格式导入
        upload.render({
            elem: '#selectFile'
            ,url: '/fulltimeTrainPoints/import'
            ,auto: false
            ,accept: "file"
            ,exts: "xls|xlsx"
            ,bindAction: '#fixImport'
            ,before: function () {
                layer.msg("正在读取文件，请稍后...")
                loadIndex = layer.load(2);
            }
            ,done: function(res){
                layer.close(loadIndex);
                if(res.code == 200){
                    layer.alert("导入成功! <br>" + res.message, {
                        icon: 1,
                        skin: 'layui-layer-molv'
                    });
                }else{

                    if(res.data){
                        layer.open({
                            type: 2,
                            title: '重名数据处理',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['820px', '620px'],
                            content: 'repeat', //iframe的url
                            btn: ['保存','取消'],
                            yes: function(index, layero){
                                var repeatData = window["layui-layer-iframe" + index].FulltimePointRepeat.getContestDatas();
                                
                                $.ajax({
                                    type: 'POST',
                                    url: Feng.ctxPath + "/fulltimeTrainPoints/adds",
                                    dataType: "json",
                                    contentType: "application/json",
                                    data: JSON.stringify(repeatData),
                                    success: function (res) {
                                        Feng.success("操作成功!");
                                        layer.closeAll();
                                    }
                                });
                                layer.close(index); //如果设定了yes回调，需进行手工关闭
                                debugger
                                layer.closeAll();
                            }

                        });
                    }else{
                        layer.alert("导入取消! <br>" + res.message, {
                            icon: 2,
                            skin: 'layui-layer-molv'
                        });
                    }

                }
                window.parent.FulltimeTrainPoints.table.refresh();

            }
        });

    });
};

/**
 * 提交校验
 */
FulltimeTrainPointsInfoDlg.formValidator = function () {

    $('.form-horizontal').bootstrapValidator({
        message: '校验失败',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            name: {
                message: '用户名验证失败',
                validators: {
                    notEmpty: {
                        message: '员工姓名不能为空'
                    }
                }
            }
        }
    });
};

FulltimeTrainPointsInfoDlg.template = function(){
    $("#template").click(function () {
        window.open(Feng.ctxPath + "/static/excel/脱产培训积分模板.xlsx","_parent");
    });
};

/**
 * 选择员工
 */
FulltimeTrainPointsInfoDlg.selectEmp = function (obj) {

    var url = Feng.ctxPath +'/employee/select_list';
    if(obj){
        url = url + "?empName="+$("#empName").val();
    }
    var index =layer.open({
        type: 2,
        area: ['800px', '500px'],
        fixed: false, //不固定

        maxmin: true,
        content: url,
        btn: ['确定'],
        yes: function(index, layero){
            var res = window["layui-layer-iframe" + index].Employee.callbackdata();
            $("#empName").val(res.empName);
            $("#empCode").val(res.empCode);
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        }
    });
    this.layerIndex = index;
};


$(function() {
    FulltimeTrainPointsInfoDlg.formValidator();
    FulltimeTrainPointsInfoDlg.upload();
    FulltimeTrainPointsInfoDlg.ruleCal();
    FulltimeTrainPointsInfoDlg.twoLevelChange();
    FulltimeTrainPointsInfoDlg.template();
});
